package com.hotel.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hotel.entity.Role;
import com.hotel.entity.UserRole;
import com.hotel.service.RoleService;

@Controller
public class RoleController {
	@Autowired
	private RoleService roleService;
	/**
	 * 查询角色列表
	 * @param request
	 * @return
	 */
	@RequestMapping(value="roleList")
	public ModelAndView findAllRole(HttpServletRequest request,
			@RequestParam(value="pageNo",defaultValue="1")Integer pageNo,
			@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		ModelAndView roleView = new ModelAndView();
		List<Role> roles = roles(null,null, pageNo,pageSize);
		roleView.addObject("roles",roles);
		roleView.setViewName("views/admin/role/roleList");
		return roleView;
	}
	
	/**
	 * 按条件查询角色
	 * @param userId	用户ID
	 * @param choose	查询条件
	 * @param pageNo	页码
	 * @param pageSize	页容量
	 * @return	用户ID的角色集合
	 */
	@RequestMapping(value="roles", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public List<Role> roles(Integer userId,String choose,
			@RequestParam(value="pageNo",defaultValue="1")Integer pageNo,
			@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		List<Role> roles = roleService.findRoleByUserId(userId,choose,pageNo,pageSize);
		return roles;
	}
	
	@RequestMapping(value="roleAllot")
	public ModelAndView RoleAllot(HttpServletRequest request){
		ModelAndView roleView = new ModelAndView();
		/*List<Role> roles = roleService.findAllRole();
		roleView.addObject("roles",roles);*/
		roleView.setViewName("views/admin/role/roleAllot");
		return roleView;
	}
	

	/**
	 * 修改角色名称
	 * @param request
	 * @param role
	 * @return 修改结果 yes:成功 no:失败
	 */
	@RequestMapping(value="roleUpdate")
	@ResponseBody
	public String RoleUpdate(HttpServletRequest request,Role role){
		return roleService.updateByPrimaryKey(role)==0 ? "no" : "yes";
	}
	/**
	 * 查询全部用户以及用户对应的角色
	 * @param request
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="userRoles")
	public ModelAndView findRolesByUser(HttpServletRequest request,@RequestParam(value="pageNo",defaultValue="1")Integer pageNo,
			@RequestParam(value="pageSize",defaultValue="10")Integer pageSize){
		ModelAndView roleView = new ModelAndView();
		List<UserRole> userrole = roleService.findRolesByUser(pageNo,pageSize,null);
		roleView.addObject("userrole",userrole);
		roleView.addObject("pageNo",pageNo);
		roleView.addObject("pageSize",pageSize);
		roleView.setViewName("views/admin/role/roleAllot");
		return roleView;
	}
	
	/**
	 * 添加角色
	 * @param role
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="insertRole",method=RequestMethod.POST)
	public String insertRole(Role role){
		return roleService.insert(role)==0?"no":"yes";
	}
	/**
	 * 用户授予角色
	 * @param request
	 * @param userRole
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="insertUserRole",method=RequestMethod.POST)
	public String insertUserRole(HttpServletRequest request,Integer userId,Integer roleId){
		return roleService.insertUserRole(userId,roleId)>=1?"yes":"no";
	}
	/**
	 * 删除角色
	 * @param roleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="deleteRolebyId",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String deleteRolebyId(Integer roleId){
		//删除之前检查用户是否存在
		if(roleService.findByRoleId(roleId)!=null){
			return "ex";
		}
		return roleService.deleteByPrimaryKey(roleId)==1?"yes":"no";
	};
	/**
	 * 
	 * 撤销用户的角色
	 * @param userRoleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="revokeRole",method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public String revokeRole(Integer userId,Integer roleId){
		String result = "no";
		UserRole ur = roleService.findByUserRole(userId,roleId);
		if(ur!=null){
			result = roleService.deleteByUserRoleId(ur.getUserroleId())==1?"yes":"no";
		}
		return result;
	}
	
}
